package com.wn.ziroombackend.controller;

import com.wn.ziroombackend.entity.Room;
import com.wn.ziroombackend.entity.RoomImg;
import com.wn.ziroombackend.service.RoomImgService;
import com.wn.ziroombackend.service.RoomService;
import com.wn.ziroombackend.utils.FtpUtil;
import com.wn.ziroombackend.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;

@RestController
public class UploadController {

    @Autowired
    private RoomImgService roomImgService;

    @Autowired
    private RoomService roomService;

    @RequestMapping("/uploadRoomImgs")
    public R uploadRoomsImg(MultipartFile file, Integer roomId, @RequestParam(defaultValue = "") String filePath) throws IOException {

        String fileName = file.getOriginalFilename();
        String suffix = fileName.substring(fileName.lastIndexOf("."));
        fileName = UUID.randomUUID() + suffix;


        InputStream inputStream = file.getInputStream();
        //上传到服务器
        FtpUtil.uploadFile("139.224.186.148", 21, "781974288", "ekwJdKiTWSFwG2YZ", "/",
                filePath, fileName, inputStream);

        //图片名称保存到数据库
        inputStream.close();
        RoomImg roomImg = new RoomImg();
        roomImg.setRoomId(roomId);
        roomImg.setPath(fileName);
        roomImgService.save(roomImg);
        return R.ok("ok");
    }


    @RequestMapping("/uploadRoomImg")
    public R uploadRoomImg(MultipartFile file, Integer id, String filePath) throws IOException {

        String fileName = file.getOriginalFilename();
        String suffix = fileName.substring(fileName.lastIndexOf("."));
        fileName = UUID.randomUUID() + suffix;


        InputStream inputStream = file.getInputStream();
        //上传到服务器
        FtpUtil.uploadFile("139.224.186.148", 21, "781974288", "ekwJdKiTWSFwG2YZ", "/",
                filePath, fileName, inputStream);
        //保存到数据库
        inputStream.close();
        Room room = new Room();
        room.setId(id);
        room.setImg(fileName);
        roomService.updateById(room);

        return R.ok("ok").data("fileName", fileName);
    }

    @RequestMapping("/uploadImg")
    public R uploadImg(MultipartFile file) throws IOException {

        String fileName = file.getOriginalFilename();
        String suffix = fileName.substring(fileName.lastIndexOf("."));
        fileName = UUID.randomUUID() + suffix;


        InputStream inputStream = file.getInputStream();
        //上传到服务器
        boolean f = FtpUtil.uploadFile("139.224.186.148", 21, "781974288", "ekwJdKiTWSFwG2YZ", "",
                "/", fileName, inputStream);
        if(f){
            return R.ok("ok").data("fileName", fileName);
        }else {
            return R.error("上传失败");
        }
    }
}
